package com.amsp.oa.oademo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.amsp.oa.oademo.entity.MachineEquipment;
import com.amsp.oa.oademo.entity.MachineMaintenance;
import com.amsp.oa.oademo.service.MachineEquipmentService;
import com.amsp.oa.oademo.service.MachineMaintenanceService;
import com.amsp.oa.oademo.vo.MachineWithMaintenanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

@Api(tags = "机器设备管理")
@RestController
@RequestMapping("/api/machine-equipment")
public class MachineEquipmentController {

    @Autowired
    private MachineEquipmentService machineEquipmentService;

    @Autowired
    private MachineMaintenanceService machineMaintenanceService;

    @ApiOperation("分页查询机器设备")
    @GetMapping("/page")
    public Page<MachineEquipment> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String machineName,
            @RequestParam(required = false) String cardNumber) {

        Page<MachineEquipment> page = new Page<>(current, size);
        LambdaQueryWrapper<MachineEquipment> wrapper = new LambdaQueryWrapper<>();

        if (machineName != null && !machineName.isEmpty()) {
            wrapper.like(MachineEquipment::getMachineName, machineName);
        }
        if (cardNumber != null && !cardNumber.isEmpty()) {
            wrapper.like(MachineEquipment::getCardNumber, cardNumber);
        }

        return machineEquipmentService.page(page, wrapper);
    }

    @ApiOperation("获取机器设备详情")
    @GetMapping("/{id}")
    public MachineEquipment getById(@PathVariable Long id) {
        return machineEquipmentService.getById(id);
    }

    @ApiOperation("保存或更新机器设备")
    @PostMapping("/save")
    public MachineEquipment saveOrUpdate(@RequestBody MachineEquipment machineEquipment) {
        machineEquipmentService.saveOrUpdate(machineEquipment);
        return machineEquipment; // MyBatis-Plus会自动填充ID
    }

    @ApiOperation("删除机器设备")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        // 删除关联的维护记录
        LambdaQueryWrapper<MachineMaintenance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MachineMaintenance::getMachineId, id);
        machineMaintenanceService.remove(wrapper);

        return machineEquipmentService.removeById(id);
    }

    @ApiOperation("批量删除机器设备")
    @DeleteMapping("/batch")
    public boolean deleteBatch(@RequestBody List<Long> ids) {
        // 删除关联的维护记录
        LambdaQueryWrapper<MachineMaintenance> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(MachineMaintenance::getMachineId, ids);
        machineMaintenanceService.remove(wrapper);

        return machineEquipmentService.removeByIds(ids);
    }

    @ApiOperation("保存机器设备和维护记录")
    @PostMapping("/save-with-maintenance")
    @Transactional
    public MachineWithMaintenanceVO saveWithMaintenance(@RequestBody MachineWithMaintenanceVO vo) {
        // 保存机器设备
        MachineEquipment machineEquipment = vo.getMachineEquipment();
        machineEquipmentService.saveOrUpdate(machineEquipment);

        // 保存维护记录
        List<MachineMaintenance> records = vo.getMaintenanceRecords();
        if (records != null && !records.isEmpty()) {
            records.forEach(record -> record.setMachineId(machineEquipment.getId()));
            machineMaintenanceService.saveOrUpdateBatch(records);
        }

        // 返回保存后的数据
        vo.setMachineEquipment(machineEquipment);
        vo.setMaintenanceRecords(records);
        return vo;
    }
}